package com.zh.rent.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.zh.rent.pojo.AlipayConfig;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ToAlipay {
    public String toAlipay(String code,String AccountType,Double BuyMoney,String Voucher,Integer InvitationDays) throws Exception{
        AlipayConfig alipayConfig = new AlipayConfig();
        //封装rsa签名方式
        //参数1：请求网关
        //2，收款人id
        //3，支付宝私钥
        //4，返回格式
        //5，字符编码格式
        //6，支付宝公钥
        //7，加密方式
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig.getGatewayUrl(),
                alipayConfig.getApp_id(), alipayConfig.getMerchant_private_key(),
                "json", alipayConfig.getCharset(),
                alipayConfig.getAlipay_public_key(), alipayConfig.getSign_type());
        //创建Request请求
        //AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest ();
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest ();
        //AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = code;
        //付款金额，必填
        String total_amount = String.valueOf(BuyMoney);
        //订单名称，必填
        String subject =AccountType+"账号购买";
        //商品描述，可空
        String body = "用户订购账号天数："+InvitationDays;
        //封装传入参数
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
        model.setOutTradeNo(out_trade_no);
        model.setSubject(subject);
        model.setTotalAmount(total_amount);
        model.setBody(body);
        model.setTimeoutExpress("2m");
       /* model.setTimeoutExpress(timeout_express);
        model.setQrCodeTimeoutExpress(product_code);*/
        // model.setProductCode(product_code);
        //设置参数
        request.setBizModel(model);
        /*request.setBizContent("{\"out_trade_no\":\""+WIDout_trade_no+"\","
                + "\"total_amount\":\""+WIDtotal_amount+"\","
                + "\"subject\":\""+WIDsubject+"\","
                + "\"body\":\""+body+"\","
                + "\"timeout_express\":\""+timeout_express+"\","
                + "\"product_code\":\""+product_code+"\"}");*/
        //设置异步回调地址
        request.setNotifyUrl(alipayConfig.getNotify_url());
        //设置同步回调地址
        request.setReturnUrl(alipayConfig.getReturn_url());
        String form = null;
        AlipayTradePrecreateResponse response=null;
        try {
            //form = alipayClient.pageExecute(request).getBody();
            response = alipayClient.execute (request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        System.out.println(response.getBody ().toString());
        Map<String, String> params = response.getParams();
        System.out.println("params:"+params.toString());
        String outTradeNo = response.getOutTradeNo();
        return response.getBody();
    }
}
